﻿
namespace GameToolkit
{
    public abstract class Disposable : System.IDisposable, IValidator
    {
        readonly object _dispose_lock = new object();
        bool mDisposed;
        public bool IsValid
        {
            get
            {
                lock (_dispose_lock)
                {
                    return !mDisposed;
                }
            }
        }

        protected object GetSyncLock() { return _dispose_lock; }

        public Disposable()
        {
        }

        ~Disposable()
        {
            Dispose();
        }

        protected abstract void OnDisposing();

        public void Dispose()
        {
            lock (_dispose_lock)
            {
                if (!mDisposed)
                {
                    mDisposed = true;
                    OnDisposing();
                }
            }
        }
    }
}
